package com.itheima.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class JwtUtils {
    private static String SIGHKEY = "itheima";
    private static long EXPIRE = 3600 * 1000;

    public static String generateJwt(Integer id, String name, Integer role, String field) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", id);
        claims.put("name", name);
        claims.put("field", field);
        claims.put("role", role);
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, SIGHKEY)
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .compact();
        return jwt;
    }

    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(SIGHKEY)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }


}
